package com.happle.gcmclient.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.happle.gcmclient.R;
import com.happle.gcmclient.obj.MessageObj;
import com.happle.gcmclient.utility.DateTimeUtility;

public class MessageAdapter extends BaseAdapter {

	private String TAG = "MessageAdapter";
	private Activity context;
	private List<MessageObj> listMessageObj;
	private static LayoutInflater inflater = null;

	public MessageAdapter(Activity activity, List<MessageObj> listMessageObj) {
		context = activity;
		this.listMessageObj = listMessageObj;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return listMessageObj.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		MessageObj item = listMessageObj.get(position);
		// if (convertView == null) {
		holder = new ViewHolder();
		convertView = inflater.inflate(R.layout.message_row, null);
		holder.lblContent = (TextView) convertView
				.findViewById(R.id.message_row_text);
		holder.lblDate = (TextView) convertView
				.findViewById(R.id.message_row_date);
		convertView.setTag(holder);
		// }
		if (item != null) {
			holder = (ViewHolder) convertView.getTag();
			holder.lblDate.setText(DateTimeUtility
					.reformatDateTimeWithPhoneSetting(context,
							item.get_datetime()));
			holder.lblContent.setText(item.get_message());
			// if (item.is_is_owner()) {
			//holder.lblDate.setBackgroundColor(context.getResources().getColor(R.color._white));
			//if (position % 2 == 1) {
            if (item.is_is_owner()) {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
                params.gravity = Gravity.LEFT;
                holder.lblDate.setLayoutParams(params);
                params.setMargins(0, 0, 50, 0);
                holder.lblContent.setLayoutParams(params);
                holder.lblContent
                        .setBackgroundResource(R.drawable.chat_bub_left);
                holder.lblContent.setPadding(20, 10,15,10);
			} else {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
                params.gravity = Gravity.RIGHT;
                holder.lblDate.setLayoutParams(params);
                params.setMargins(50,0,0,0);
                holder.lblContent.setLayoutParams(params);
                holder.lblContent
                        .setBackgroundResource(R.drawable.chat_bub_right);
                holder.lblContent.setPadding(15,10,20,10);
			}

		} else {
			Log.i(TAG, "Null Object !");
		}
		return convertView;

	}
}

class ViewHolder {
	TextView lblContent;
	TextView lblDate;
}